﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using EquipSecs300;
using SecsEquip;
using SecsEquip300;

namespace EquipSecs300
{
    public partial class LoadPortSubView_SJ : CMyFormEx
    {
        int m_nPortNo = 0;
        public GEM_LoadPortSub GEMLoadPortSub { get; set; }
        string CarrierID { get; set; } = "";

        public LoadPortSubView_SJ()
        {
            m_pViewName = "Carrier";

            InitializeComponent();
        }

        // 设置端口号
        public void SetPortNo(int nPortNo)
        {
            m_nPortNo = nPortNo;
        }

        public int GetPortNo()
        {
            return m_nPortNo;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                //textBoxTransferState.Text = GEMLoadPortSub.loadPortTransferState_KE.ToString();
                textBoxCarrierIDStatus.Text = GEMLoadPortSub.CarrierStatus.ToString();
                //textBoxCarrierSlotStatus.Text = GEMLoadPortSub.carrierSlotMapStatus_KE.ToString();

                CompState();
                textBoxPPID.Text = GEMLoadPortSub.RecipeID_SJ;
                textBoxLotID.Text = GEMLoadPortSub.LotID_SJ;
                textBoxIsStart.Text = GEMLoadPortSub.StrStart;
            }
        }

        private string strTransferStatebuf;
        private string strTransferState;
        PortStatus_KE EnuTransfertate;
        int intTransfertate;
        void CompState()
        {
            secsEquipHelper.Variables.GetValue(10190 + m_nPortNo - 1, ref strTransferState);
            intTransfertate = UtilitiesConvert.StringToInt32(strTransferState);
            EnuTransfertate = (PortStatus_KE)intTransfertate;
            strTransferState = EnuTransfertate.ToString();
            if (!strTransferState.Equals(strTransferStatebuf))
            {
                strTransferStatebuf = strTransferState;
                textBoxTransferState.Text = strTransferState;
            }
        }

        // 其他内容处理

        // 准备上料、下料
        private void btnReadyLoadUnload_Click(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierInLoadPort.Checked;
            if (bCheck)
            {
                // 有料
                GEMLoadPortSub.Ev_LoadPortUnloadReady(PortFoupStatus_KE.ReadyUnload, CarrierID);
            }
            else
            {
                // 没有料
                GEMLoadPortSub.Ev_LoadPortLoadReady(PortFoupStatus_KE.Readyload, "");
            }
        }

        // 放有料在Load Port
        private void checkBoxCarrierInLoadPort_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierInLoadPort.Checked;
            if (bCheck)
            {
                //有料
                GEMLoadPortSub.Ev_LoadPortLoaded(PortFoupStatus_KE.Loaded, CarrierID);

            }
            else
            {
                //无料
                GEMLoadPortSub.Ev_LoadPortUnLoaded(PortFoupStatus_KE.UnLoaded, CarrierID);

            }
        }

        // Load Port是否锁紧料盒
        private void checkBoxCarrierClamp_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierClamp.Checked;
            if (bCheck)
            {
                // 锁紧
                GEMLoadPortSub.Ev_BoxCarrierClamped(PortFoupStatus_KE.Clamp, CarrierID);
            }
            else
            {
                // 解锁
                GEMLoadPortSub.Ev_BoxCarrierUnClamped(PortFoupStatus_KE.UnClamp, CarrierID);
            }
        }

        // 料盒CmsLoadPortDocked
        private void BoxCarrierDocked_CheckedChanged(object sender, EventArgs e)
        {
            //bool bCheck = BoxCarrierDocked.Checked;
            //if (bCheck)
            //{
            //    GEMLoadPortSub.BoxCarrierDocked();
            //}
            //else
            //{
            //    GEMLoadPortSub.BoxCarrierUnDocked();
            //}
        }

        // 料盒门打开情况
        private void checkBoxCarrierOpen_CheckedChanged(object sender, EventArgs e)
        {
            bool bCheck = checkBoxCarrierOpen.Checked;
            if (bCheck)
            {
                // 打开
                GEMLoadPortSub.Ev_BoxCarrierOpened(PortFoupStatus_KE.CarrierOpened, CarrierID);
            }
            else
            {
                // 关闭
                GEMLoadPortSub.Ev_BoxCarrierClosed(PortFoupStatus_KE.CarrierClosed, CarrierID);
            }
        }

        // 读取料盒ID成功
        private void BtnReadCarrierSucc_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.CarrierStatus = "";
            string pID = textBoxCarrierID.Text;
            if (pID == null ||
                pID.Length == 0)
            {
                MessageBox.Show("Carrier ID为空");
            }
            else
            {
                GEMLoadPortSub.Ev_ReadCarrierSucc(PortFoupStatus_KE.Loaded, pID);
            }
        }

        // 读取料盒ID失败
        private void BtnReadCarrierFail_Click(object sender, EventArgs e)
        {
            //GEMLoadPortSub.ReadCarrierFail();
        }

        // 读取Slot成功
        private void BtnReadCarrierSlotSucc_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.CarrierStatus = "";
            string pID = textBoxCarrierID.Text;
            if (pID == null ||
                pID.Length == 0)
            {
                MessageBox.Show("Carrier ID为空");
            }
            else
            {
                List<ushort> list = new List<ushort>();
                for (int i = 0; i < 25; i++)
                {
                    if (i == 1 || i == 5)
                    {
                        list.Add(1);
                    }
                    else
                    {
                        list.Add(0);
                    }
                }

                GEMLoadPortSub.Ev_ReadCarrierSlotSucc(PortFoupStatus_KE.MappingNormal, pID, list);
            }
        }

        private void btnRecipeCaneSucc_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(GEMLoadPortSub.RecipeID_SJ) || string.IsNullOrEmpty(GEMLoadPortSub.LotID_SJ))
            {
                MessageBox.Show("RecipeID或者LotID");
                return;
            }
            secsEquipHelper.Even.Ev_RecipeChangeSucc(GEMLoadPortSub.RecipeID_SJ, GEMLoadPortSub.LotID_SJ, new List<int>());
        }

        private void btnProcessStar_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_BoxCarrierProcessStart(PortFoupStatus_KE.FoupWorking, CarrierID);
        }

        private void btnProcessEnd_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_BoxCarrierProcessEnd(PortFoupStatus_KE.ReadyUnload, CarrierID);
        }

        private void btnTrigReset_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.Ev_LoadPortLoadReady_R();
            GEMLoadPortSub.Ev_LoadPortUnloadReady_R();
            GEMLoadPortSub.Ev_LoadPortLoaded_R();
            GEMLoadPortSub.Ev_BoxCarrierClamped_R();
            GEMLoadPortSub.Ev_ReadCarrierSucc_R();
            GEMLoadPortSub.Ev_ReadCarrierFail_R();
            GEMLoadPortSub.Ev_BoxCarrierOpened_R();
            GEMLoadPortSub.Ev_BoxCarrierClosed_R();
            GEMLoadPortSub.Ev_BoxCarrierUnClamped_R();
            GEMLoadPortSub.Ev_LoadPortUnLoaded_R();
            GEMLoadPortSub.Ev_ReadCarrierSlotSucc_R();
            GEMLoadPortSub.Ev_RecipeChangeSucc_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessStart_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessEnd_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessStart_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessEnd_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessStart_R();
            GEMLoadPortSub.Ev_BoxCarrierProcessEnd_R();
        }

        private void textBoxCarrierID_TextChanged(object sender, EventArgs e)
        {
            CarrierID = textBoxCarrierID.Text;
        }

        private void btnSamplesConfirm_Click(object sender, EventArgs e)
        {
            GEMLoadPortSub.SamplesConfirm = true;
        }
    }
}
